package com.university.report.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @ClassName health_report
 * @Author Jinondo
 * @Date 2021/6/20 11:43
 */
public class TimeUtils {
    public static Date stringToDate(String dateStr){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Long stringToTime(String dateStr){
        Date date = stringToDate(dateStr);
        return date.getTime();
    }
    public static Date timeToDate(Long time){
        Date date = new Date();
        date.setTime(time);
        return date;
    }
    public static String timeToString(Long time){
        Date date = timeToDate(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    /**
     * 获取 今天零点零分零秒 和 今天23点59分59秒
     * @return
     */
    public static List<String> getTodays(){
        //当前时间毫秒数
        long current=System.currentTimeMillis();
        //今天零点零分零秒的毫秒数
        long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();
        // 今天23点59分59秒的毫秒数
        long twelve=zero+24*60*60*1000-1;
        String from = DateFormatUtils.format(new Date(zero), "yyyy-MM-dd HH:mm:ss");
        String to = DateFormatUtils.format(new Date(twelve), "yyyy-MM-dd HH:mm:ss");
        ArrayList<String> todays = new ArrayList<String>(2) {{
            add(from);
            add(to);
        }};
        return todays;
    }

    /**
     * 获取 某天零点零分零秒 和 某天 23点59分59秒
     * @return
     */
    public static List<String> getOnedays(String timeString){
        timeString = timeString + " 00:00:00";
        //当前时间毫秒数
        Long zero = stringToTime(timeString);
        //今天零点零分零秒的毫秒数
        // 今天23点59分59秒的毫秒数
        long twelve=zero+24*60*60*1000-1;
        String from = DateFormatUtils.format(new Date(zero), "yyyy-MM-dd HH:mm:ss");
        String to = DateFormatUtils.format(new Date(twelve), "yyyy-MM-dd HH:mm:ss");
        ArrayList<String> oneDays = new ArrayList<String>(2) {{
            add(from);
            add(to);
        }};
        return oneDays;
    }

}
